using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PlugNT.Safe;
using PlugNT.Database.Common;
using PlugNT.Database.Common.Simple;
using PlugNT.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class FriendLinkClass
    {


        private static string currTableName = WebConfig.TablePrefix + "friendlink_class";







        public FriendLinkClassInfo GetFriendLinkClassInfo(string clsno)
        {
            DataTable dt = GetFriendLinkClassTable(clsno);
            FriendLinkClassInfo model = null;
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                model = new FriendLinkClassInfo();
                model.id = (int)dr["id"];
                model.clsno = dr["clsno"].ToString();
                model.clsname = dr["clsname"].ToString();
            }
            return model;

        }

        public string GetTitle(string clsno)
        {
            string sql="Select top 1 clsname From " +  currTableName+" where clsno='"+StringHelper.SqlFilter(clsno)+"'";
            object obj = DbHelper.GetSingle(sql);
            return (obj == null) ? "" : obj.ToString();
        }






        public DataTable GetFriendLinkClassTable(string clsno)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Select top 1 * From " +  currTableName);
            strSql.Append(" Where clsno='" + StringHelper.SqlFilter(clsno) + "'");
            DataTable dt = DbHelper.TabQuery(strSql.ToString());
            return dt;
        }









        public bool Add(FriendLinkClassInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " +  currTableName);
            strSql.Append(" (clsno,clsname) values (");
            strSql.Append("'" + StringHelper.SqlFilter(model.clsno) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.clsname) + "'");
            strSql.Append(")");

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool Update(FriendLinkClassInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " +  currTableName);
            strSql.Append(" set clsname='" + StringHelper.SqlFilter(model.clsname) + "'");
            strSql.Append(" where clsno='" + StringHelper.SqlFilter(model.clsno) + "' ");
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool Delete(string clsno)
        {

            FriendLink.ExtRemoveLinks(clsno);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " +  currTableName);
            strSql.Append(" where clsno= '" + StringHelper.SqlFilter(clsno) + "'");
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }









        public DataTable GetList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select clsno,clsname from " +  currTableName);
            return DbHelper.TabQuery(strSql.ToString());
        }







        public DataTable GetList(string clsno)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select clsno,clsname from " +  currTableName);
            strSql.Append(" where clsno='" + StringHelper.SqlFilter(clsno) + "' ");
            return DbHelper.TabQuery(strSql.ToString());
        }

    }
}
